from sqlalchemy.orm import Session
from . import models, schemas

def get_ims_modules_bindings(db: Session, eid: int):
    return db.query(models.IMSModulesBindings).filter(models.IMSModulesBindings.eid == eid).first()

def get_ims_modules_bindings_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesBindings).offset(skip).limit(limit).all()

def create_ims_modules_bindings(db: Session, ims_modules_bindings: schemas.IMSModulesBindingsCreate):
    db_ims_modules_bindings = models.IMSModulesBindings(**ims_modules_bindings.dict())
    db.add(db_ims_modules_bindings)
    db.commit()
    db.refresh(db_ims_modules_bindings)
    return db_ims_modules_bindings

def update_ims_modules_bindings(db: Session, eid: int, ims_modules_bindings: schemas.IMSModulesBindingsCreate):
    db_ims_modules_bindings = db.query(models.IMSModulesBindings).filter(models.IMSModulesBindings.eid == eid).first()
    if db_ims_modules_bindings:
        for var, value in vars(ims_modules_bindings).items():
            setattr(db_ims_modules_bindings, var, value) if value else None
        db.add(db_ims_modules_bindings)
        db.commit()
        db.refresh(db_ims_modules_bindings)
    return db_ims_modules_bindings

def delete_ims_modules_bindings(db: Session, eid: int):
    db_ims_modules_bindings = db.query(models.IMSModulesBindings).filter(models.IMSModulesBindings.eid == eid).first()
    if db_ims_modules_bindings:
        db.delete(db_ims_modules_bindings)
        db.commit()
    return db_ims_modules_bindings

def get_ims_modules_cloud(db: Session, id: int):
    return db.query(models.IMSModulesCloud).filter(models.IMSModulesCloud.id == id).first()

def get_ims_modules_cloud_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesCloud).offset(skip).limit(limit).all()

def create_ims_modules_cloud(db: Session, ims_modules_cloud: schemas.IMSModulesCloudCreate):
    db_ims_modules_cloud = models.IMSModulesCloud(**ims_modules_cloud.dict())
    db.add(db_ims_modules_cloud)
    db.commit()
    db.refresh(db_ims_modules_cloud)
    return db_ims_modules_cloud

def update_ims_modules_cloud(db: Session, id: int, ims_modules_cloud: schemas.IMSModulesCloudCreate):
    db_ims_modules_cloud = db.query(models.IMSModulesCloud).filter(models.IMSModulesCloud.id == id).first()
    if db_ims_modules_cloud:
        for var, value in vars(ims_modules_cloud).items():
            setattr(db_ims_modules_cloud, var, value) if value else None
        db.add(db_ims_modules_cloud)
        db.commit()
        db.refresh(db_ims_modules_cloud)
    return db_ims_modules_cloud

def delete_ims_modules_cloud(db: Session, id: int):
    db_ims_modules_cloud = db.query(models.IMSModulesCloud).filter(models.IMSModulesCloud.id == id).first()
    if db_ims_modules_cloud:
        db.delete(db_ims_modules_cloud)
        db.commit()
    return db_ims_modules_cloud

def get_ims_modules_ignore(db: Session, id: int):
    return db.query(models.IMSModulesIgnore).filter(models.IMSModulesIgnore.id == id).first()

def get_ims_modules_ignore_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesIgnore).offset(skip).limit(limit).all()

def create_ims_modules_ignore(db: Session, ims_modules_ignore: schemas.IMSModulesIgnoreCreate):
    db_ims_modules_ignore = models.IMSModulesIgnore(**ims_modules_ignore.dict())
    db.add(db_ims_modules_ignore)
    db.commit()
    db.refresh(db_ims_modules_ignore)
    return db_ims_modules_ignore

def update_ims_modules_ignore(db: Session, id: int, ims_modules_ignore: schemas.IMSModulesIgnoreCreate):
    db_ims_modules_ignore = db.query(models.IMSModulesIgnore).filter(models.IMSModulesIgnore.id == id).first()
    if db_ims_modules_ignore:
        for var, value in vars(ims_modules_ignore).items():
            setattr(db_ims_modules_ignore, var, value) if value is not None else None
        db.add(db_ims_modules_ignore)
        db.commit()
        db.refresh(db_ims_modules_ignore)
    return db_ims_modules_ignore

def delete_ims_modules_ignore(db: Session, id: int):
    db_ims_modules_ignore = db.query(models.IMSModulesIgnore).filter(models.IMSModulesIgnore.id == id).first()
    if db_ims_modules_ignore:
        db.delete(db_ims_modules_ignore)
        db.commit()
    return db_ims_modules_ignore


def get_ims_modules_plugin(db: Session, id: int):
    return db.query(models.IMSModulesPlugin).filter(models.IMSModulesPlugin.id == id).first()

def get_ims_modules_plugin_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesPlugin).offset(skip).limit(limit).all()

def create_ims_modules_plugin(db: Session, ims_modules_plugin: schemas.IMSModulesPluginCreate):
    db_ims_modules_plugin = models.IMSModulesPlugin(**ims_modules_plugin.dict())
    db.add(db_ims_modules_plugin)
    db.commit()
    db.refresh(db_ims_modules_plugin)
    return db_ims_modules_plugin

def update_ims_modules_plugin(db: Session, id: int, ims_modules_plugin: schemas.IMSModulesPluginCreate):
    db_ims_modules_plugin = db.query(models.IMSModulesPlugin).filter(models.IMSModulesPlugin.id == id).first()
    if db_ims_modules_plugin:
        for var, value in vars(ims_modules_plugin).items():
            setattr(db_ims_modules_plugin, var, value) if value is not None else None
        db.add(db_ims_modules_plugin)
        db.commit()
        db.refresh(db_ims_modules_plugin)
    return db_ims_modules_plugin

def delete_ims_modules_plugin(db: Session, id: int):
    db_ims_modules_plugin = db.query(models.IMSModulesPlugin).filter(models.IMSModulesPlugin.id == id).first()
    if db_ims_modules_plugin:
        db.delete(db_ims_modules_plugin)
        db.commit()
    return db_ims_modules_plugin


def get_ims_modules_plugin_rank(db: Session, id: int):
    return db.query(models.IMSModulesPluginRank).filter(models.IMSModulesPluginRank.id == id).first()

def get_ims_modules_plugin_rank_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesPluginRank).offset(skip).limit(limit).all()

def create_ims_modules_plugin_rank(db: Session, ims_modules_plugin_rank: schemas.IMSModulesPluginRankCreate):
    db_ims_modules_plugin_rank = models.IMSModulesPluginRank(**ims_modules_plugin_rank.dict())
    db.add(db_ims_modules_plugin_rank)
    db.commit()
    db.refresh(db_ims_modules_plugin_rank)
    return db_ims_modules_plugin_rank

def update_ims_modules_plugin_rank(db: Session, id: int, ims_modules_plugin_rank: schemas.IMSModulesPluginRankCreate):
    db_ims_modules_plugin_rank = db.query(models.IMSModulesPluginRank).filter(models.IMSModulesPluginRank.id == id).first()
    if db_ims_modules_plugin_rank:
        for var, value in vars(ims_modules_plugin_rank).items():
            setattr(db_ims_modules_plugin_rank, var, value) if value is not None else None
        db.add(db_ims_modules_plugin_rank)
        db.commit()
        db.refresh(db_ims_modules_plugin_rank)
    return db_ims_modules_plugin_rank

def delete_ims_modules_plugin_rank(db: Session, id: int):
    db_ims_modules_plugin_rank = db.query(models.IMSModulesPluginRank).filter(models.IMSModulesPluginRank.id == id).first()
    if db_ims_modules_plugin_rank:
        db.delete(db_ims_modules_plugin_rank)
        db.commit()
    return db_ims_modules_plugin_rank


def get_ims_modules_rank(db: Session, id: int):
    return db.query(models.IMSModulesRank).filter(models.IMSModulesRank.id == id).first()

def get_ims_modules_rank_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesRank).offset(skip).limit(limit).all()

def create_ims_modules_rank(db: Session, ims_modules_rank: schemas.IMSModulesRankCreate):
    db_ims_modules_rank = models.IMSModulesRank(**ims_modules_rank.dict())
    db.add(db_ims_modules_rank)
    db.commit()
    db.refresh(db_ims_modules_rank)
    return db_ims_modules_rank

def update_ims_modules_rank(db: Session, id: int, ims_modules_rank: schemas.IMSModulesRankCreate):
    db_ims_modules_rank = db.query(models.IMSModulesRank).filter(models.IMSModulesRank.id == id).first()
    if db_ims_modules_rank:
        for var, value in vars(ims_modules_rank).items():
            setattr(db_ims_modules_rank, var, value) if value is not None else None
        db.add(db_ims_modules_rank)
        db.commit()
        db.refresh(db_ims_modules_rank)
    return db_ims_modules_rank

def delete_ims_modules_rank(db: Session, id: int):
    db_ims_modules_rank = db.query(models.IMSModulesRank).filter(models.IMSModulesRank.id == id).first()
    if db_ims_modules_rank:
        db.delete(db_ims_modules_rank)
        db.commit()
    return db_ims_modules_rank


def get_ims_modules_recycle(db: Session, id: int):
    return db.query(models.IMSModulesRecycle).filter(models.IMSModulesRecycle.id == id).first()

def get_ims_modules_recycle_all(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.IMSModulesRecycle).offset(skip).limit(limit).all()

def create_ims_modules_recycle(db: Session, ims_modules_recycle: schemas.IMSModulesRecycleCreate):
    db_ims_modules_recycle = models.IMSModulesRecycle(**ims_modules_recycle.dict())
    db.add(db_ims_modules_recycle)
    db.commit()
    db.refresh(db_ims_modules_recycle)
    return db_ims_modules_recycle

def update_ims_modules_recycle(db: Session, id: int, ims_modules_recycle: schemas.IMSModulesRecycleCreate):
    db_ims_modules_recycle = db.query(models.IMSModulesRecycle).filter(models.IMSModulesRecycle.id == id).first()
    if db_ims_modules_recycle:
        for var, value in vars(ims_modules_recycle).items():
            setattr(db_ims_modules_recycle, var, value) if value is not None else None
        db.add(db_ims_modules_recycle)
        db.commit()
        db.refresh(db_ims_modules_recycle)
    return db_ims_modules_recycle

def delete_ims_modules_recycle(db: Session, id: int):
    db_ims_modules_recycle = db.query(models.IMSModulesRecycle).filter(models.IMSModulesRecycle.id == id).first()
    if db_ims_modules_recycle:
        db.delete(db_ims_modules_recycle)
        db.commit()
    return db_ims_modules_recycle


# Modules CRUD
def get_module(db: Session, mid: int):
    return db.query(models.Modules).filter(models.Modules.mid == mid).first()

def get_modules(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.Modules).offset(skip).limit(limit).all()

def create_module(db: Session, module: schemas.ModulesCreate):
    db_module = models.Modules(**module.dict())
    db.add(db_module)
    db.commit()
    db.refresh(db_module)
    return db_module

def update_module(db: Session, mid: int, module: schemas.ModulesUpdate):
    db_module = db.query(models.Modules).filter(models.Modules.mid == mid).first()
    if db_module:
        for var, value in vars(module).items():
            setattr(db_module, var, value) if value is not None else None
        db.add(db_module)
        db.commit()
        db.refresh(db_module)
    return db_module

def delete_module(db: Session, mid: int):
    db_module = db.query(models.Modules).filter(models.Modules.mid == mid).first()
    if db_module:
        db.delete(db_module)
        db.commit()
    return db_module